1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package com.henri.jbcs;
22
23 import java.util.ArrayList;
24 import java.util.Collection;
25 import java.util.Iterator;
26 import java.util.List;
27
28 import javax.swing.Action;
29
30 import com.borland.jbuilder.node.JBProject;
31 import com.borland.jbuilder.node.JavaFileNode;
32 import com.borland.jbuilder.node.PackageNode;
33 import com.borland.primetime.ide.Browser;
34 import com.borland.primetime.ide.BrowserAction;
35 import com.borland.primetime.ide.ContextActionProvider;
36 import com.borland.primetime.node.Node;
37
38 /***
39 * A BrowserAction to check multiple files.<p>
40 *
41 * The action spawns a thread for the checking, so that the message view can be
42 * updated during a lengthy checking process.
43 *
44 * @author Henri Tremblay
45 * @see BrowserAction
46 * @see ContextActionProvider
47 * @version $Revision: 1.6 $ $Date: 2005/08/01 20:48:47 $
48 */
49 public class CheckStyleContextAction extends BrowserAction implements ContextActionProvider
50 {
51 private Node[] m_nodes;
52
53 /***
54 * @see BrowserAction
55 */
56 public CheckStyleContextAction() {
57 super("Run Checkstyle", 'C',
58 "Checkstyle is a development tool to help programmers write " +
59 "Java code that adheres to a coding standard",
60 CheckStyleOpenTool.ICON, "Run Checkstyle");
61 }
62
63 /***
64 * Runs Checkstyle on all the <code>JavaFileNode</code> entries in the
65 * m_Nodes array. If the array is null, get the active Node from the
66 * browser.
67 *
68 * @param browser The active browser.
69 */
70 public void actionPerformed(Browser browser) {
71
72 if(m_nodes == null) {
73 m_nodes = new Node[] {browser.getActiveNode()};
74 }
75
76 List fileList = new ArrayList();
77 for(int i = 0; i < m_nodes.length; i++) {
78 if(m_nodes[i] instanceof JavaFileNode) {
79 if(!fileList.contains(m_nodes[i])) {
80 fileList.add(m_nodes[i]);
81 }
82 }
83 else if(m_nodes[i] instanceof PackageNode) {
84 processPackage(fileList, (PackageNode) m_nodes[i]);
85 }
86 else if(m_nodes[i] instanceof JBProject) {
87
88 m_nodes = browser.getActiveProject().getDisplayChildren();
89
90
91 for(int j = 0; j < m_nodes.length; j++) {
92 if(m_nodes[j] instanceof PackageNode && m_nodes[j].isPersistent()) {
93 m_nodes[j] = null;
94 }
95 }
96 this.actionPerformed(browser);
97 return;
98 }
99 }
100
101 JavaFileNode[] nodes = new JavaFileNode[fileList.size()];
102 Iterator it = fileList.iterator();
103 for(int i = 0; it.hasNext(); i++) {
104 nodes[i] = (JavaFileNode) it.next();
105 }
106
107 CheckStyleOpenTool.checkNodes(browser, nodes);
108
109 m_nodes = null;
110 }
111
112 private void processPackage(List fileList, PackageNode node) {
113
114
115 Node[] packageNodes = node.getDisplayChildren();
116
117
118 Collection exclusionPath = CheckStyleOpenTool.getPreference().getExclusionPath();
119
120 for(int i = 0; i < packageNodes.length; i++) {
121 if(packageNodes[i] instanceof JavaFileNode) {
122 if(isExcluded(exclusionPath, ((JavaFileNode) packageNodes[i]).getUrl().getFullName())) {
123 continue;
124 }
125 if(!fileList.contains(packageNodes[i])) {
126 fileList.add(packageNodes[i]);
127 }
128 }
129 else if(packageNodes[i] instanceof PackageNode && CheckStyleOpenTool.getPreference().getRecursive()) {
130 processPackage(fileList, (PackageNode) packageNodes[i]);
131 }
132 }
133
134 }
135
136 private boolean isExcluded(Collection exclusionPath, String filePath) {
137 for(Iterator it = exclusionPath.iterator(); it.hasNext();) {
138 String path= (String) it.next();
139 if(filePath.indexOf(path) > 0) {
140 return true;
141 }
142 }
143 return false;
144 }
145
146 /***
147 * Sets the internal <code>Node[]</code> and return the Action if the first
148 * entry is a source file, package node or project.
149 *
150 * @param browser The active browser.
151 * @param nodes The active nodes.
152 * @return A reference to the static <code>CheckStyleOpenTool.ACTION_CheckStyleProject</code>
153 * action if the node is a source file, package node or project.
154 * @see ContextActionProvider
155 */
156 public Action getContextAction(Browser browser, Node[] nodes) {
157 if(nodes.length == 0) {
158 return null;
159 }
160 if(nodes[0] instanceof JBProject || nodes[0] instanceof JavaFileNode || nodes[0] instanceof PackageNode) {
161 m_nodes = nodes;
162 return this;
163 }
164 else {
165 return null;
166 }
167 }
168 }